package cn.lxh.template;

import cn.lxh.properties.GeTuiProperties;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lxh Create Time: 2023/12/30 13:06
 * OSS对象存储工具类
 */
@Slf4j
public class GeTuiTemplate {

    private GeTuiProperties ossProperties;

    private ApiHelper apiHelper;

    public GeTuiTemplate(GeTuiProperties ossProperties, ApiHelper apiHelper) {
        this.ossProperties = ossProperties;
        this.apiHelper = apiHelper;
    }

    /**
     * 绑定别名
     *
     * @param cid   客户端id
     * @param alias 别名
     * @return
     */
    public Boolean bindingAlias(String cid, String alias) {
        // 获取到UserApi
        UserApi userApi = apiHelper.creatApi(UserApi.class);
        CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
        // 绑定别名
        cidAliasListDTO.add(new CidAliasListDTO.CidAlias(cid, alias));
        ApiResult<Void> apiResult = userApi.bindAlias(cidAliasListDTO);
        log.info("绑定别名====={}", apiResult);
        return apiResult.getCode() == 0; // 0就是绑定别名成功状态
    }

    /**
     * 通过别名推送消息
     *
     * @param message 推送的消息内容
     * @param alias 别名
     * @return
     */
    public Boolean pushMessagesThroughAlias(String message, String alias) {
        // 创建消息推送api
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(message); // 推送的消息
        pushDTO.setPushMessage(pushMessage);

        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        // audience.addCid("bb1f112923863735fad3f44e2eac279d");
        audience.setAlias(Arrays.asList(alias)); // 别名
        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);

        return apiResult.getCode() == 0; // 0就是通过别名推送消息成功状态
    }


}
